define(function(){
    var currPageObj={};
    currPageObj.init=function(){
        var self = this;
        initData();
        queryPageList(1);
    	
        currPageObj.initEvents();
    };

    //绑定事件
    currPageObj.initEvents=function(){
        var self = this;
        $(".page-info").on("click","li",function(){
    		var topage = $(this).attr("page");
    		if(topage != "#"){
    			queryPageList(topage);
    		}
    	});
        $("#deleteSel").on("click",function(){
        	deleteSel();
    	});
        
        $("#checkAll").click(function(){
    		$(".table_style1 input[type='checkbox']").prop('checked', $("#checkAll").prop('checked'));
    	});
        $("#pageSize").on("change",function(){
        	queryPageList(1);
    	});
    	
    	$("#searchBtn").click(function() {
    		queryPageList(1);
    	});
    	
    	$(".operation .operation_add").click(function() {
    		showDicAdd();
    	});
    	
    	$(".add .cancel").click(function() {
    		disshowDicEdit();
    	});
    	
    	$(".add .submit").click(function() {
    		saveOrEditDic();
    	});
    };
    return currPageObj;
});

function freshData() {
	var topage = $(".page-info .cur-page").attr("page");
	if(topage != "#" && topage !="undefined"){
		queryPageList(topage);
	}
}

var curPage=0;
var totalPage=0;
function queryPageList(currentpage) {
	var pageSize = $("#pageSize").val();
	var dicName = $("#searchDicName").val();
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/dic/getDicPageList.htm',
		data : 'dicName=' + dicName+'&currentpage=' + currentpage+'&pageSize='+pageSize,
		cache : false,
		success : function(data) {
			
			if(data.error){
				return;
			}
			
			var res =  data.data;
			var pagelist = res.datas;
			curPage = res.pageNumber;
			totalPage = res.totalPages;
			totalCount = res.total;
			var listString = "";
			$("#pageDataList").empty();
			for(var i=0; i<pagelist.length; i++){
					var listItem = pagelist[i];
					var rank = parseInt((curPage-1)*pageSize+i*1+1);
					var stDicId = listItem.stDicId;
					var stDicCode = listItem.stDicCode;
					var stDicName = listItem.stDicName;
					var stDicNamespaceValue = listItem.stDicNamespaceValue;
					var stDicNamespaceText = listItem.stDicNamespaceText;
					var nmOrder = listItem.nmOrder;
					
					if(i % 2 == 1){
						listString+="<tr class='even'>";
					}else{
						listString+=" <tr>";
					}
					listString+="    <td><input name=\"\" type=\"checkbox\" value=\""+stDicId+"\" /></td>";
					listString+="    <td >"+rank+"</td>";
					listString+="    <td><span title="+stDicNamespaceText+">"+ stDicNamespaceText+ "</span></td>";
					listString+="    <td><span title="+stDicNamespaceValue+">"+ stDicNamespaceValue+ "</span></td>";
					listString+="    <td><span title="+stDicName+">"+ stDicName+ "</span></td>";
					listString+="    <td><span title="+stDicCode+">"+ stDicCode+ "</span></td>";
					listString+="    <td><span title="+nmOrder+">"+ nmOrder+ "</span></td>";
					listString+="    <td ><span><a class=\"modify\" onclick=\"showDicEdit('"+stDicId+"')\" href='javascript:;'>编辑</a>&nbsp;&nbsp;<a class=\"modify\" onclick=\"deleteDic('"+stDicId+"')\" href='javascript:;'>删除</a></span></td>";
					listString+=" </tr>";
			}
			$("#pageDataList").append(listString);
			$("#totalCount").html(totalCount);
			setPageInfoPart(parseInt(curPage),parseInt(totalPage));
		},
		error : function() {
		}
	});
}


function showDicAdd() {
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/menu/getAllMenus.htm',
		data : '',
		cache : false,
		success : function(data) {
			var menulist = data.data;
			var listString = "<option value=\"\"></option>";
			for(var i=0; i<menulist.length; i++){
				var listItem = menulist[i];
				
				listString+="<option value=\""+listItem.stMenuId+"\">"+listItem.stMenuName+"</option>"
			}
			$("#stPid").empty();
			$("#stPid").append(listString);
			$(".add .title").text("添加菜单");
			
			$(".tan .tan_min").animate({opacity:1,"margin-top":"0"},600);
			$(".add").fadeIn();
		},
		error : function() {
		}
	});
	
}

function showDicEdit(id) {
	var data = $.ajax({
		type : 'post',
		url : 'sysmgr/dic/getDicEdit.htm',
		data : 'dicid='+id,
		cache : false,
		success : function(data) {
			var dicinfo = data.data;
			
			$("#stDicId").val(dicinfo.stDicId);
			$("#stDicName").val(dicinfo.stDicName);
			$("#stDicCode").val(dicinfo.stDicCode);
			$("#stDicNamespaceText").val(dicinfo.stDicNamespaceText);
			$("#stDicNamespaceValue").val(dicinfo.stDicNamespaceValue);
			$("#nmOrder").val(dicinfo.nmOrder);
			
			$(".add .title").text("修改字典项");
			$(".tan .tan_min").animate({opacity:1,"margin-top":"0"},600);
			$(".add").fadeIn();
		},
		error : function() {
		}
	});
	
}

function disshowDicEdit() {
	$(".tan .tan_min").animate({opacity:1,"margin-top":"-500"},600);
	$(".add").fadeOut();
	$(".add .text").val("");
}

function saveOrEditDic(){
	if(checkInput()){
		var stDicId = $("#stDicId").val().trim();
		var stDicName = $("#stDicName").val().trim();
		var stDicCode = $("#stDicCode").val().trim();
		var stDicNamespaceText = $("#stDicNamespaceText").val().trim();
		var stDicNamespaceValue = $("#stDicNamespaceValue").val().trim();
		var nmOrder = $("#nmOrder").val().trim();
		
		var data = $.ajax({
			type : 'post',
			url : 'sysmgr/dic/saveOrEditDic.htm',
			data : 'stDicId=' + stDicId+'&stDicName=' + stDicName+'&stDicCode=' + stDicCode+'&stDicNamespaceText=' + stDicNamespaceText+'&stDicNamespaceValue=' + stDicNamespaceValue+'&nmOrder='+nmOrder,
			cache : false,
			success : function(data) {
				alert(data.msg);
				freshData();
				disshowDicEdit();
			},
			error : function() {
			}
		});
	}
	
}

function deleteDic(ids){
	if(confirm("确定要删除吗？")){
		var data = $.ajax({
			type : 'post',
			url : 'sysmgr/dic/deleteDic.htm',
			data : 'dicId=' + ids,
			cache : false,
			success : function(data) {
				alert(data.msg);
				freshData();
			},
			error : function() {
			}
		});
	}
	
}

function deleteSel(){
	var idList=[];
	 $(".table_style1").find('tr :checkbox:checked').each(function(){
		 var id = $(this).attr('value');
		 idList.push(id);
	 });   
	 if(idList.length>0){
		 var ids = idList.join(',');
		 //调用删除用户接口
		 deleteDic(ids);
	 }
	 else{
		 alert("至少选择一条数据删除！");
	 }
}

function checkInput(){
	var stDicName = $("#stDicName").val();
	if(stDicName.trim() == ""){
		alert("请输入字典项名称！");
		$("#stDicName").focus();
		return false;
	}
	
	var stDicCode = $("#stDicCode").val();
	if(stDicCode.trim() == ""){
		alert("请输入字典项值！");
		$("#stDicCode").focus();
		return false;
	}
	
	var stDicNamespaceText = $("#stDicNamespaceText").val();
	if(stDicNamespaceText.trim() == ""){
		alert("请输入字典项上级名称！");
		$("#stDicNamespaceText").focus();
		return false;
	}
	
	var stDicNamespaceValue = $("#stDicNamespaceValue").val();
	if(stDicNamespaceValue.trim() == ""){
		alert("请输入字典项上级值！");
		$("#stDicNamespaceValue").focus();
		return false;
	}
	
	var nmOrder = $("#nmOrder").val();
	if(nmOrder.trim() != ""){
		if(isNaN(nmOrder)){
			alert("请输入数字！");
			$("#nmOrder").focus();
			return false;
		}
	}
	return true;
}

function initData(){
	$.ajax({
		type : 'post',
		url : 'sysmgr/dic/getDicTopList.htm',
		data : '',
		cache : false,
		success : function(data) {
			if(data.error){
				return;
			}
			
			var datalist =  data.data;
			var listString = "<option value=\"\"></option>";
			for(var i=0; i<datalist.length; i++){
				var listItem = datalist[i];
				listString+="<option value=\""+listItem.namespaceText+"\">"+listItem.namespaceText+"</option>";
			}
			$("#searchDicName").empty();
			$("#searchDicName").append(listString);
		},
		error : function() {
		}
	});
}

function mouseOver(tip)
{
	$("#"+tip).css("display","inline-block");
	}
	function mouseOut(tip)
	{
	$("#"+tip).css("display","none");
}
//设置页码
function setPageInfoPart(curPage, totalPage) {
	$(".page-info .page").remove();
	var startPage = curPage - 5;//要显示的起始页码
	var endPage = curPage + 5;
	
	if(startPage<=0){
		startPage = 1;
	}
	if(endPage >= totalPage){
		endPage = totalPage;
	}
	var showPageNum = endPage - startPage+1;//要显示的页码总数
	//添加页码元素
	var lastElem = $(".page-info .pre");
	var count = 0;
	if(startPage>1){
		$(
				"<li page=1 class='page'>1</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	if(curPage - 1>=10){
		$(
				"<li page='#' class=' page '>……</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	for ( var i = count; i < showPageNum+count; i++) {
		if (curPage == startPage) {
			$(
					"<li page=\""+startPage+"\" class=\"page cur-page\">"
							+ startPage + "</li>").insertAfter(lastElem);
		} else {
			$(
					"<li page=\""+startPage+"\" class=\"page\">"
							+ startPage + "</li>").insertAfter(lastElem);
		}
		lastElem = $(".page-info .page:eq(" + i + ")");
		startPage++;
	}
	count = i;
	
	if(totalPage - curPage >=10){
		$(
				"<li page='#' class=' page '>……</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + count + ")");
		count++;
	}
	if(endPage<totalPage){
		$(
				"<li page="+totalPage+" class='page'>"+totalPage+"</li>").insertAfter(lastElem);
		lastElem = $(".page-info .page:eq(" + totalPage + ")");
		count++;
	}
	//设置首页，上一页，下一页，最后一页页码
	if(curPage == 1){
		$(".page-info .first").css("display","none");
		$(".page-info .pre").css("display","none");
	}else{
		$(".page-info .first").css("display","inline-block");
		$(".page-info .pre").css("display","inline-block");
		
		
		$(".page-info .first").attr("page", 1);
		$(".page-info .pre")
				.attr("page", (curPage - 1) > 0 ? (curPage - 1) : 1);
	}
	if(curPage == totalPage){
		
		$(".page-info .next").css("display","none");
		$(".page-info .last").css("display","none");
	}else{
		$(".page-info .next").css("display","inline-block");
		$(".page-info .last").css("display","inline-block");
		$(".page-info .next").attr("page",
				(curPage + 1) > totalPage ? totalPage : (curPage + 1));
		$(".page-info .last").attr("page", totalPage);
	}

}

function FormatDate (strTime) {
	if(strTime.trim() == "") return "";
	var date = new Date(Date.parse(strTime.replace(/-/g,"/")));
	return date.Format("yyyy-MM-dd hh:mm:ss");
}


